import sys
import logging
import sqlalchemy
from lxml import etree
from xadmin.model import *
from xadmin.lib.base import *

log = logging.getLogger(__name__)

class IndicatorsController(BaseController):
    """REST Controller styled on the Atom Publishing Protocol"""
    # To properly map this controller, ensure your config/routing.py file has
    # a resource setup:
    #     map.resource('indicator', 'indicators')


    def index(self, format='html'):
        """GET /indicators: All items in the collection."""
        # url_for('indicators')
        response.headers['Content-Type'] = 'text/xml; charset=utf-8'
        return render('/indicators/indicators.mako')

    def listByProject(self, project):
        project = Session.query(Project).get(project)
        if project is not None:
            c.total_cr = len(project.client_requirements)
            c.total_cr_without_ta = 0
            for cr in project.client_requirements:
                if len(cr.aceptance_tests) <= 0:
                    c.total_cr_without_ta = c.total_cr_without_ta + 1
            priorities = Session.query(Priority).all()
            impacts = Session.query(Impact).all()
            statuses = Session.query(Status).all()
            types = Session.query(Type).all()
            c.cr_by_priority = {}
            for priority in priorities:
                c.cr_by_priority[priority.name] = len(Session.query(ClientRequirement).filter_by(project_id=project.id, priority_id=priority.id).all())
            c.cr_by_impact = {}
            for impact in impacts:
                c.cr_by_impact[impact.name] = len(Session.query(ClientRequirement).filter_by(project_id=project.id, impact_id=impact.id).all())
            c.cr_by_status = {}
            for status in statuses:
                c.cr_by_status[status.name] = len(Session.query(ClientRequirement).filter_by(project_id=project.id, status_id=status.id).all())
            c.cr_by_type = {}
            for type in types:
                c.cr_by_type[type.name] = len(Session.query(ClientRequirement).filter_by(project_id=project.id, type_id=type.id).all())
            c.acceptance_tests = len(project.aceptance_tests)
            response.headers['Content-Type'] = 'text/xml; charset=utf-8'
            return render('/indicators/indicators.mako')
        else:
            abort(404)

    def create(self):
        """POST /indicators: Create a new item."""
        # url_for('indicators')
        pass

    def new(self, format='html'):
        """GET /indicators/new: Form to create a new item."""
        # url_for('new_indicator')
        pass

    def update(self, id):
        """PUT /indicators/id: Update an existing item."""
        # Forms posted to this method should contain a hidden field:
        #    <input type="hidden" name="_method" value="PUT" />
        # Or using helpers:
        #    h.form(h.url_for('indicator', id=ID),
        #           method='put')
        # url_for('indicator', id=ID)
        pass

    def delete(self, id):
        """DELETE /indicators/id: Delete an existing item."""
        # Forms posted to this method should contain a hidden field:
        #    <input type="hidden" name="_method" value="DELETE" />
        # Or using helpers:
        #    h.form(h.url_for('indicator', id=ID),
        #           method='delete')
        # url_for('indicator', id=ID)
        pass

    def show(self, id, format='html'):
        """GET /indicators/id: Show a specific item."""
        # url_for('indicator', id=ID)
        pass

    def edit(self, id, format='html'):
        """GET /indicators/id;edit: Form to edit an existing item."""
        # url_for('edit_indicator', id=ID)
        pass
